//
//  GDNetWorkTool.m
//  爱新奇－味道
//
//  Created by 热带雨林 on 17/2/18.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDNetWorkTool.h"
#import <AFNetworking.h>

@implementation GDNetWorkTool{
    AFHTTPSessionManager *_maneger;
}

static id _instance;
+ (instancetype)shardNetWorkTool{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}
- (instancetype)init{
    if(self = [super init]){
        
        //要保证sessionManager 唯一的实例
        //        _manager = [AFHTTPSessionManager manager];
        //基地址
        //http://c.m.163.com/nc/ad/headline/0-4.html
        //http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html
        
        //http://c.m.163.com/nc/article/list/T1348648517839/0-20.html(新)
        //http://c.m.163.com/nc/article/list/T1348649079062/0-20.html
        
        NSString *strUrl = @"http://c.m.163.com/nc";
        
        NSURL *url = [NSURL URLWithString:strUrl];
        _maneger = [[AFHTTPSessionManager alloc]initWithBaseURL:url];
        
    }
    return self;
    
}
//通用的办法
- (void)getRuqestWithURL:(NSString *)urlString parameters:(NSDictionary *)param complete:(void(^)(id responsOBJ, NSError *error))completeBlock{
    [_maneger GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        //请求数据成功 就回调block
        if(completeBlock){
            completeBlock(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        NSLog(@"%@",error);
        
    }];
}
- (void)getNewListWithTID:(NSString *)tid complete:(void(^)(id respondOBJ))completeBlock{
    
    //http://c.m.163.com/nc/article/C637G3PV000380BQ/full.html
    //http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html
    
    //由tid拼接出一个URL
    NSString *strURL = [NSString stringWithFormat:@"article/headline/%@/0-140.html",tid];
    [self getRuqestWithURL:strURL parameters:nil complete:^(id responsOBJ, NSError *error) {
        
        NSArray *array = responsOBJ[tid];
        if(completeBlock){
            //如果成功就将数组传出去
            completeBlock(array);
        }
    }];
    
}

//获得详情页中的数据
- (void)getNewDetailsWebWithDOCID:(NSString *)cid complete:(void(^)(id respondOBJ))completeBlock{
    //http://c.m.163.com/nc/article/C637G3PV000380BQ/full.html
    NSString *strURL = [NSString stringWithFormat:@"article/%@/full.html",cid];
    [self getRuqestWithURL:strURL parameters:nil complete:^(id responsOBJ, NSError *error) {
        NSDictionary *dic = responsOBJ[cid];
        if(completeBlock){
            completeBlock(dic);
        }
    }];
    
}



@end
